//var stores;
var xhr = new XMLHttpRequest(); 
xhr.open('get', '../js/data.json'); 
xhr.send(); 
xhr.addEventListener('readystatechange', function() { 
	if(xhr.readyState==4&&xhr.status==200){
		stores = JSON.parse(xhr.responseText);
		return stores
	}
})

//初始化地图
    var map = new AMap.Map('container',{
        resizeEnable: true,
        zoom: 10
    });
//得到自己的坐标
    var geolocation;
    map.plugin('AMap.Geolocation', function() {
        geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位，默认:true
            timeout: 10000,          //超过10秒后停止定位，默认：无穷大
//          buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
//          zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
//          buttonPosition:'RB'
        });
        map.addControl(geolocation);
        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
        AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
    });
    //解析定位结果
    function onComplete(data) {
    	console.log('经度：' + data.position.getLng());
    	console.log('纬度：' + data.position.getLat());
		
		//  new AMap.Marker({
		//      map: map,
		//      position: [116.368904, 39.923423]
		//  });
		new AMap.Marker({
		    map: map,
		    position: [data.position.getLng(), data.position.getLat()]
		});
		map.setFitView();
	    
    //遍历得到最近商店坐标
//  stores.sort(function(a,b){
////		得到两点距离
//		var distance_a = Math.pow((a.JD-data.position.getLng()),2)+Math.pow((a.WD-data.position.getLat()),2);
//		var distance_b = Math.pow((b.JD-data.position.getLng()),2)+Math.pow((b.WD-data.position.getLat()),2);
//		return distance_a - distance_b
//  }) 
    
    stores.sort(function(a,b){
//		得到两点距离
//		 var lnglat = new AMap.LngLat(data.position.getLng(), data.position.getLat());
//		console.log(lnglat)
		var distance_a = Math.pow((a.JD-data.position.getLng()),2)+Math.pow((a.WD-data.position.getLat()),2);
		var distance_b = Math.pow((b.JD-data.position.getLng()),2)+Math.pow((b.WD-data.position.getLat()),2);
		return distance_a - distance_b
    }) 
    console.log(stores[0])
    
		//创建距离最近的坐标的marker
		    var marker = new AMap.Marker({
		        position:[stores[0].JD,stores[0].WD]
		    });
		    marker.setMap(map);
		    marker.on('click',function(e){
		      infowindow.open(map,e.target.getPosition());
		    })
		    AMap.plugin('AMap.AdvancedInfoWindow',function(){
		       infowindow = new AMap.AdvancedInfoWindow({
		//      content: '<div class="info-title">高德地图</div><div class="info-content">'+
		//              '<img src="https://webapi.amap.com/images/amap.jpg">'+
		//              '高德是中国领先的数字地图内容、导航和位置服务解决方案提供商。<br/>'+
		//              '<a target="_blank" href = "https://mobile.amap.com/">点击下载高德地图</a></div>',
		        offset: new AMap.Pixel(0, -20)
		      });
		      infowindow.open(map,[stores[0].JD,stores[0].WD]);
		    })
		    AMap.plugin(['AMap.ToolBar','AMap.Scale'],function(){
		        var toolBar = new AMap.ToolBar();
		        var scale = new AMap.Scale();
		//      map.addControl(toolBar);
		//      map.addControl(scale);
		//      map.addControl(geolocation);
		    })
		    map.setFitView();
    }
    //解析定位错误信息
    function onError(data) {
        alert('定位失败');
    }
